﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Diagnostics;

namespace NRainel.Log
{
    /// <summary>
    /// Windows事件日志
    /// </summary>
    public class EventLogRecord : IRecordLog
    {

        string _logName;
        string _logSource;
        //public EventLogRecord(string logSource, string errorLogName, string logName)
        //    : this(logSource, errorLogName)
        //{
        //    if (!EventLog.SourceExists(logSource))
        //        EventLog.CreateEventSource(logSource, logName);

        //    _log = new EventLog(logName);
        //    _log.Source = _logSource;
        //}

        public EventLogRecord(string logSource, string logName)
        {
            if (!EventLog.SourceExists(logSource))
                EventLog.CreateEventSource(logSource, logName);

            _logName = logName;
            _logSource = logSource;


        }

        #region IRecordLog 成员

        public void RecordLog(string log, LogTypeEnum logType)
        {
            using (EventLog logObj = new EventLog(_logName))
            {
                logObj.Source = _logSource + "Source";
                //logObj.Source = "aaa";
                switch (logType)
                {
                    case LogTypeEnum.Error:
                        logObj.WriteEntry(log, EventLogEntryType.Error);
                        break;
                    case LogTypeEnum.Info:
                        if (logObj != null)
                        {
                            logObj.WriteEntry(log, EventLogEntryType.Information);
                        }
                        break;
                }
                
            }
        }

        #endregion
    }
}
